/**
 * Copyright (Warren)
 * FileName: _5_LongestPalindromicSubstring
 * Author:
 * Date:     2019/3/13 0:58
 * Description:
 * History:
 */
package leetcode;

public class _5_LongestPalindromicSubstring {
    private static int maxLen = 0;
    private static String sub = "";
    class Solution {
        public String longestPalindrome(String s) {
            if (s.length() <= 1) return s;
            for (int i = 0; i < s.length()-1; i++) {
                findLongestPalingdrome(s,i,i);
                findLongestPalingdrome(s,i,i+1);
            }
            return sub;
        }
        public  void findLongestPalingdrome(String s,int low,int high){
            while (low >= 0 && high <= s.length()-1){
                if (s.charAt(low) == s.charAt(high)){
                    if (high - low + 1 > maxLen){
                        maxLen = high - low + 1;
                        sub = s.substring(low, high+1);
                    }
                    low --;
                    high ++;
                }else {
                    break;
                }
            }
        }
    }

    public Solution getSolution(){
        return new Solution();
    }

    public static void  main(String [] args){
        _5_LongestPalindromicSubstring c = new _5_LongestPalindromicSubstring();
        _5_LongestPalindromicSubstring.Solution s = c.getSolution();
        String ss = "abaccbd";
        String res = s.longestPalindrome(ss);
        System.out.println("result:"+res);
    }
}
